﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Frame.Lib.Data;

namespace Frame.Lib.Utilities
{
    public sealed class EntityUtil
    {
        private EntityUtil() { }

        /// <summary>
        /// 把控件数据放入实体当中
        /// </summary>
        /// <param name="page"></param>
        /// <param name="info"></param>
        public static void SetEntity(Panel panel, object info)
        {
            Type type = info.GetType();
            foreach (PropertyInfo property in type.GetProperties())
            {
                Control ctl = panel.FindControl(property.Name);
                if (ctl != null)
                {
                    Type valueType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

                    TypeCode typeCode = Type.GetTypeCode(valueType);

                    object value = GetControlValue(ctl, typeCode);

                    property.SetValue(info, value, null);
                }
            }
        }

        public static void SetEntity(EntityBase info)
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            Type type = info.GetType();
            foreach (PropertyInfo property in type.GetProperties())
            {
                string frmValue = context.Request.Form[property.Name];
                if (!string.IsNullOrEmpty(frmValue))
                {
                    Type valueType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

                    TypeCode typeCode = Type.GetTypeCode(valueType);

                    object value = GetValue(frmValue, typeCode);//类型转换
                    info.SetValue(property.Name, value);
                }
            }
        }

        private static object GetValue(string src, TypeCode typeCode)
        {
            object value = null;

            switch (typeCode)
            {

                case TypeCode.Int32:
                    value = Convert.ToInt32(src);
                    break;
                case TypeCode.Boolean:
                    if (src == "0")
                    {
                        value = false;
                    }
                    else if (src == "1")
                    {
                        value = true;
                    }
                    else
                    {
                        value = Convert.ToBoolean(src);
                    }
                    break;
                case TypeCode.DateTime:
                    value = Convert.ToDateTime(src);                    
                    break;
                case TypeCode.Single:

                    value = Convert.ToSingle(src);
                   
                    break;
                case TypeCode.Double:
                    value = Convert.ToDouble(src);                    
                    break;
                case TypeCode.Decimal:
                    value = Convert.ToDecimal(src);                    
                    break;
                case TypeCode.String:
                    value = src;
                    break;
            }


            return value;
        }

        private static object GetControlValue(Control ctl, TypeCode typeCode)
        {
            Type type = ctl.GetType();
            string strType = type.Name;
            PropertyInfo pro = null;
            switch (strType)
            {
                case "Literal":
                case "Label":
                case "TextBox":
                    pro = type.GetProperty("Text");
                    break;
                case "HtmlInputText":
                case "HtmlInputHidden":
                case "HiddenField":
                    pro = type.GetProperty("Value");
                    break;
                case "DropDownList":
                case "RadioButtonList":
                    pro = type.GetProperty("SelectedValue");
                    break;
                case "CheckBox":
                case "HtmlInputCheckBox":
                    pro = type.GetProperty("Checked");
                    break;
                case "WYSIWYGEditor":
                    pro = type.GetProperty("Content");
                    break;
            }
            object value = pro.GetValue(ctl, null) + "";
            if (type == typeof(CheckBox) || type == typeof(HtmlInputCheckBox))
            {
                value = bool.Parse(value.ToString());
            }

            value = GetValue(value.ToString(), typeCode);

            return value;
        }

        /// <summary>
        /// 把实体数据放入控件当中
        /// </summary>
        /// <param name="page"></param>
        /// <param name="info"></param>
        public static void SetControl(Control panel, object info)
        {
            Type type = info.GetType();
            foreach (PropertyInfo property in type.GetProperties())
            {
                Control ctl = panel.FindControl(property.Name);
                if (ctl != null)
                {
                    object value = property.GetValue(info, null) + "";
                    if (value != null)
                    {
                        SetControlValue(ctl, value);
                    }
                }
            }
        }
        private static void SetControlValue(Control ctl, object value)
        {
            Type type = ctl.GetType();
            string strType = type.Name;
            PropertyInfo pro = null;
            switch (strType)
            {
                case "Literal":
                case "Label":
                case "TextBox":
                    pro = type.GetProperty("Text");
                    break;
                case "HtmlInputText":
                case "HtmlInputHidden":
                case "HiddenField":
                    pro = type.GetProperty("Value");
                    break;
                case "DropDownList":
                case "RadioButtonList":
                    pro = type.GetProperty("SelectedValue");
                    break;
                case "CheckBox":
                case "HtmlInputCheckBox":
                    if (value.ToString() != "")
                    {
                        int result;
                        if (int.TryParse(value.ToString(), out result))
                        {
                            value = (result == 1) ? true : false;
                        }
                        else
                        {
                            value = Convert.ToBoolean(value.ToString());
                        }
                        pro = type.GetProperty("Checked");
                    }
                    break;
                case "WYSIWYGEditor":
                    pro = type.GetProperty("Content");
                    break;
            }
            if (pro != null)
            {
                pro.SetValue(ctl, value, null);
            }
        }
    }
}
